/*
 * NumericalUtilities.cpp
 *
 *  Created on: 7 Apr 2011
 *      Author: al2010
 */

#include "NumericalUtilities.h"

const double LagrangeInterpolation(const double& x, const double* xpoints, const double* ypoints, const unsigned npoints)
{
	double y = 0.0;

	for(unsigned i = 0; i < npoints; ++i)
	{
		double lagrangei = 1.0;

		for(unsigned j = 0; j < npoints; ++j)
		{
			lagrangei *= (i == j) ? 1.0 : (x - xpoints[j])/(xpoints[i] - xpoints[j]);
		}

		y += ypoints[i] * lagrangei;
	}

	return y;
}

const double NumericalDerivative(const double (*f)(double), double x, double epsilon)
{
	return 0.5/epsilon * (f(x + epsilon) - f(x - epsilon));
}

const double InnerProduct(const vector<double>& u, const vector<double>& v)
{
	double res = 0.0; 
	
	for(unsigned i = 0; i < u.size(); ++i) 
		res += u[i] * v[i];
	
	return res;
}
